package com.ymatou.iapi.productcategory.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.mongo.BuyerProductWapper;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.DiffUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.ymatou.iapi.productcategory.parameter.*;
import com.ymatou.iapi.productcategory.service.*;

/**
 * FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetProductCategorysByCategoryIds {
	private static GetProductCategorysByCategoryIdsBean GetProductCategorysByCategoryIdsBean;
	private static GetProductCategorysByCategoryIdsCall GetProductCategorysByCategoryIdsCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据规格Id获取商品品类信息");
	}

	@Before
	public void caseUp() {
		GetProductCategorysByCategoryIdsBean = new GetProductCategorysByCategoryIdsBean();
		// 旧站点
		// GetProductCategorysByCategoryIdsCall=new
		// GetProductCategorysByCategoryIdsCall(EnvSetup.getSystemUrl("productcategory_api"));
		// 新站点
		GetProductCategorysByCategoryIdsCall = new GetProductCategorysByCategoryIdsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(GetProductCategorysByCategoryIdsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductCategorysByCategoryIds_001() {
		Logger.start(true, "根据规格Id获取商品品类信息");
		try {
			BuyerProductWapper np = new BuyerProductWapper();
			ArrayList<Map> productCategoryInfoList = null;
			//这里有时候会连接池关闭？
			try {
				productCategoryInfoList = np.getProductCategoryInfoLimit(5);
			} catch (Exception e) {
				Thread.sleep(1000);
				productCategoryInfoList = np.getProductCategoryInfoLimit(5);
			}
			Logger.debug(productCategoryInfoList);
			List<Integer> categoryIds = new ArrayList<>();
			for (Map map : productCategoryInfoList) {
				Integer CategoryId = Integer.valueOf(map.get("CategoryId").toString());
				categoryIds.add(CategoryId);
			}

			GetProductCategorysByCategoryIdsBean.setCategoryIds(categoryIds);
			GetProductCategorysByCategoryIdsCall.setData(GetProductCategorysByCategoryIdsBean);
			GetProductCategorysByCategoryIdsCall.callService();
			Logger.verifyEquals("200", GetProductCategorysByCategoryIdsCall.getString("Code"), "验证Code");
			JsonArray ja = GetProductCategorysByCategoryIdsCall.getData();
			for (JsonElement jsonElement : ja) {
				ProductCategoryDto pd = (ProductCategoryDto) FormatUtil.GSONToObject(jsonElement.toString(), ProductCategoryDto.class);
				Map<String, Object> m = np.getProductCatalogInfoByCid(pd.getCategoryId().toString()).get(0);
				Logger.verifyEquals(m.get("CategoryEnName"), pd.getCategoryEnName(), "验证CategoryEnName");
				Logger.verifyEquals(m.get("CategoryName"), pd.getCategoryName(), "验证CategoryName");
				Logger.verifyEquals(m.get("CategorySort"), pd.getCategorySort(), "验证CategorySort");
				Logger.verifyEquals(m.get("MasterCategoryId"), pd.getMasterCategoryId(), "验证MasterCategoryId");
				Logger.verifyEquals(m.get("Level"), pd.getLevel(), "验证Level");
				Logger.verifyEquals(m.get("ProductsNum"), pd.getProductsNum(), "验证ProductsNum");
				int lv = pd.getLevel();
				if (lv == 2) {
					Map rm = np.getProductCatalogInfoByCid(pd.getRootCategoryId().toString()).get(0);
				}
				if (lv == 3) {
					Map rm = np.getProductCatalogInfoByCid(pd.getRootCategoryId().toString()).get(0);
					Map sm = np.getProductCatalogInfoByCid(pd.getSecondCategoryId().toString()).get(0);
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductCategorysByCategoryIds_002() {
		Logger.start(true, "批量对比根据规格Id获取商品品类信息");
		try {
			BuyerProductWapper np = new BuyerProductWapper();
			ArrayList<Map> productCategoryInfoList = null;
			//这里有时候会连接池关闭？
			try {
				productCategoryInfoList = np.getProductCategoryInfoLimit(5);
			} catch (Exception e) {
				Thread.sleep(1000);
				productCategoryInfoList = np.getProductCategoryInfoLimit(5);
			}
			Logger.debug(productCategoryInfoList);
			List<Integer> categoryIds = new ArrayList<>();
			for (Map map : productCategoryInfoList) {
				Integer CategoryId = Integer.valueOf(map.get("CategoryId").toString());
				categoryIds.add(CategoryId);
			}

			GetProductCategorysByCategoryIdsBean.setCategoryIds(categoryIds);
			GetProductCategorysByCategoryIdsCall.setData(GetProductCategorysByCategoryIdsBean);
			GetProductCategorysByCategoryIdsCall.callService();
			Logger.verifyEquals("200", GetProductCategorysByCategoryIdsCall.getString("Code"), "验证Code");
			GetProductCategorysByCategoryIdsCall oldGetProductCategorysByCategoryIdsCall = new GetProductCategorysByCategoryIdsCall(EnvSetup.getSystemUrl("productcategory_api"));
			oldGetProductCategorysByCategoryIdsCall.setData(GetProductCategorysByCategoryIdsBean);
			oldGetProductCategorysByCategoryIdsCall.callService();
//			DiffUtil diff = new DiffUtil();
//			String difffile = "diff.txt";
//			diff.setLogFile(difffile);
//			List<String> list = new ArrayList<>();
//			// list.add("Data.BrandList.Level");
//			diff.setIgnoreList(list);
//			diff.compareJsonString(oldGetProductCategorysByCategoryIdsCall.getReturnData(), GetProductCategorysByCategoryIdsCall.getReturnData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}